package org.gap.dao;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.gap.domain.PersistentLogins;

import org.skyway.spring.util.dao.AbstractJpaDao;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

/**
 * DAO to manage PersistentLogins entities.
 * 
 */
@Repository("PersistentLoginsDAO")
@Transactional
public class PersistentLoginsDAOImpl extends AbstractJpaDao<PersistentLogins>
		implements PersistentLoginsDAO {

	/**
	 * Set of entity classes managed by this DAO.  Typically a DAO manages a single entity.
	 *
	 */
	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(Arrays.asList(new Class<?>[] { PersistentLogins.class }));

	/**
	 * EntityManager injected by Spring for persistence unit 
	 *
	 */
	@PersistenceContext(unitName = "")
	private EntityManager entityManager;

	/**
	 * Instantiates a new PersistentLoginsDAOImpl
	 *
	 */
	public PersistentLoginsDAOImpl() {
		super();
	}

	/**
	 * Get the entity manager that manages persistence unit 
	 *
	 */
	public EntityManager getEntityManager() {
		return entityManager;
	}

	/**
	 * Returns the set of entity classes managed by this DAO.
	 *
	 */
	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	/**
	 * JPQL Query - findPersistentLoginsBySeries
	 *
	 */
	@Transactional
	public PersistentLogins findPersistentLoginsBySeries(String series) throws DataAccessException {

		return findPersistentLoginsBySeries(series, -1, -1);
	}

	/**
	 * JPQL Query - findPersistentLoginsBySeries
	 *
	 */

	@Transactional
	public PersistentLogins findPersistentLoginsBySeries(String series, int startResult, int maxRows) throws DataAccessException {
		try {
			return executeQueryByNameSingleResult("findPersistentLoginsBySeries", series);
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findPersistentLoginsByTokenContaining
	 *
	 */
	@Transactional
	public Set<PersistentLogins> findPersistentLoginsByTokenContaining(String token) throws DataAccessException {

		return findPersistentLoginsByTokenContaining(token, -1, -1);
	}

	/**
	 * JPQL Query - findPersistentLoginsByTokenContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<PersistentLogins> findPersistentLoginsByTokenContaining(String token, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findPersistentLoginsByTokenContaining", startResult, maxRows, token);
		return new LinkedHashSet<PersistentLogins>(query.getResultList());
	}

	/**
	 * JPQL Query - findAllPersistentLoginss
	 *
	 */
	@Transactional
	public Set<PersistentLogins> findAllPersistentLoginss() throws DataAccessException {

		return findAllPersistentLoginss(-1, -1);
	}

	/**
	 * JPQL Query - findAllPersistentLoginss
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<PersistentLogins> findAllPersistentLoginss(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllPersistentLoginss", startResult, maxRows);
		return new LinkedHashSet<PersistentLogins>(query.getResultList());
	}

	/**
	 * JPQL Query - findPersistentLoginsBySeriesContaining
	 *
	 */
	@Transactional
	public Set<PersistentLogins> findPersistentLoginsBySeriesContaining(String series) throws DataAccessException {

		return findPersistentLoginsBySeriesContaining(series, -1, -1);
	}

	/**
	 * JPQL Query - findPersistentLoginsBySeriesContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<PersistentLogins> findPersistentLoginsBySeriesContaining(String series, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findPersistentLoginsBySeriesContaining", startResult, maxRows, series);
		return new LinkedHashSet<PersistentLogins>(query.getResultList());
	}

	/**
	 * JPQL Query - findPersistentLoginsByToken
	 *
	 */
	@Transactional
	public Set<PersistentLogins> findPersistentLoginsByToken(String token) throws DataAccessException {

		return findPersistentLoginsByToken(token, -1, -1);
	}

	/**
	 * JPQL Query - findPersistentLoginsByToken
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<PersistentLogins> findPersistentLoginsByToken(String token, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findPersistentLoginsByToken", startResult, maxRows, token);
		return new LinkedHashSet<PersistentLogins>(query.getResultList());
	}

	/**
	 * JPQL Query - findPersistentLoginsByUsernameContaining
	 *
	 */
	@Transactional
	public Set<PersistentLogins> findPersistentLoginsByUsernameContaining(String username) throws DataAccessException {

		return findPersistentLoginsByUsernameContaining(username, -1, -1);
	}

	/**
	 * JPQL Query - findPersistentLoginsByUsernameContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<PersistentLogins> findPersistentLoginsByUsernameContaining(String username, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findPersistentLoginsByUsernameContaining", startResult, maxRows, username);
		return new LinkedHashSet<PersistentLogins>(query.getResultList());
	}

	/**
	 * JPQL Query - findPersistentLoginsByLastUsed
	 *
	 */
	@Transactional
	public Set<PersistentLogins> findPersistentLoginsByLastUsed(java.util.Calendar lastUsed) throws DataAccessException {

		return findPersistentLoginsByLastUsed(lastUsed, -1, -1);
	}

	/**
	 * JPQL Query - findPersistentLoginsByLastUsed
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<PersistentLogins> findPersistentLoginsByLastUsed(java.util.Calendar lastUsed, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findPersistentLoginsByLastUsed", startResult, maxRows, lastUsed);
		return new LinkedHashSet<PersistentLogins>(query.getResultList());
	}

	/**
	 * JPQL Query - findPersistentLoginsByUsername
	 *
	 */
	@Transactional
	public Set<PersistentLogins> findPersistentLoginsByUsername(String username) throws DataAccessException {

		return findPersistentLoginsByUsername(username, -1, -1);
	}

	/**
	 * JPQL Query - findPersistentLoginsByUsername
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<PersistentLogins> findPersistentLoginsByUsername(String username, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findPersistentLoginsByUsername", startResult, maxRows, username);
		return new LinkedHashSet<PersistentLogins>(query.getResultList());
	}

	/**
	 * JPQL Query - findPersistentLoginsByPrimaryKey
	 *
	 */
	@Transactional
	public PersistentLogins findPersistentLoginsByPrimaryKey(String series) throws DataAccessException {

		return findPersistentLoginsByPrimaryKey(series, -1, -1);
	}

	/**
	 * JPQL Query - findPersistentLoginsByPrimaryKey
	 *
	 */

	@Transactional
	public PersistentLogins findPersistentLoginsByPrimaryKey(String series, int startResult, int maxRows) throws DataAccessException {
		try {
			return executeQueryByNameSingleResult("findPersistentLoginsByPrimaryKey", series);
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * Used to determine whether or not to merge the entity or persist the entity when calling Store
	 * @see store
	 * 
	 *
	 */
	public boolean canBeMerged(PersistentLogins entity) {
		return true;
	}
}
